package laspon.vvvlike.school.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import laspon.vvvlike.school.dao.AdminDao;
import laspon.vvvlike.school.dto.BaseResponse;
import laspon.vvvlike.school.dto.ProductEnum;
import laspon.vvvlike.school.dto.resquse.AdminAccountDto;
import laspon.vvvlike.school.entity.Admin;
import laspon.vvvlike.school.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author by xqcw9 on 2020/9/11 14:21
 */
@Service
public class AdminServiceImpl implements AdminService {
    private static final Logger logger = LoggerFactory.getLogger(AdminServiceImpl.class);

    private AdminDao adminDao;

    @Autowired
    public void setAdminDao(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse adminAccount(AdminAccountDto adminAccountDto) {
        String msg = JSON.toJSONString(adminAccountDto);
        logger.info("admin表入参{}", msg);
        BaseResponse response = new BaseResponse();
        int flag = adminAccountDto.getFlag();

        switch (flag) {
            case 0:
                deleteAccount(adminAccountDto, response);
                break;
            case 1:
                saveAccount(adminAccountDto, response);
                break;
            case 2:
                updateAccount(adminAccountDto, response);
                break;
            default:
                response.fail();
                logger.info("admin表入参异常，请重新尝试！");
        }
        String msgs = JSON.toJSONString(response);
        logger.info("AdminService服务返参{}", msgs);
        return response;
    }

    private BaseResponse deleteAccount(AdminAccountDto adminAccountDto, BaseResponse response) {
        logger.info("admin表Flag=0");

        if (0 == adminAccountDto.getId()) {
            response.setRtnCode(ProductEnum.DELETE_NOT_ID.getCode());
            response.setRtnMsg(ProductEnum.DELETE_NOT_ID.getName());
            logger.info("删除操作未输入id");
            return response;
        }
        adminDao.deleteById(adminAccountDto.getId());

        response.setRtnCode(ProductEnum.SUCCESS.getCode());
        response.setRtnMsg(ProductEnum.SUCCESS.getName());
        response.setFlag("0-删除");
        return response;
    }


    private BaseResponse saveAccount(AdminAccountDto adminAccountDto, BaseResponse response) {
        if (StringUtils.isBlank(adminAccountDto.getName()) || StringUtils.isBlank(adminAccountDto.getPassword())){
            response.setRtnCode(ProductEnum.NO_DATA.getCode());
            response.setRtnMsg(ProductEnum.NO_DATA.getName());
        }

        logger.info("admin表Flag=1，保存");

        Admin admin = new Admin();
        admin.setId(adminAccountDto.getId());
        admin.setName(adminAccountDto.getName());
        admin.setPassword(adminAccountDto.getPassword());
        admin.setCreateTime(new Date());
        admin.setUpdateTime(admin.getCreateTime());
        adminDao.insert(admin);

        response.setRtnCode(ProductEnum.SUCCESS.getCode());
        response.setRtnMsg(ProductEnum.SUCCESS.getName());
        response.setFlag("1-保存");
        return response;
    }

    private BaseResponse updateAccount(AdminAccountDto adminAccountDto, BaseResponse response) {

        logger.info("admin表Flag=2，更新");

        if (0 == adminAccountDto.getId()) {
            response.setRtnCode(ProductEnum.UPDATE_NOT_ID.getCode());
            response.setRtnMsg(ProductEnum.UPDATE_NOT_ID.getName());
            logger.info("更新操作未输入id");
            return response;
        }

        Admin admin = new Admin();
        admin.setId(adminAccountDto.getId());
        admin.setName(adminAccountDto.getName());
        admin.setPassword(adminAccountDto.getPassword());
        admin.setUpdateTime(new Date());
        adminDao.updateById(admin);

        response.setRtnCode(ProductEnum.SUCCESS.getCode());
        response.setRtnMsg(ProductEnum.SUCCESS.getName());
        response.setFlag("2-更新");
        return response;
    }


}
